import Vue from "vue";
import VueRouter from "vue-router";
import hr_login from "@/page/hrpages/hr_login.vue";
import hr_register from "@/page/hrpages/hr_register.vue";
import hr_main_page from "@/page/hrpages/hr_main_page.vue";
import user_login from "@/page/userpages/user_login.vue";
import user_register from "@/page/userpages/user_register.vue";
import user_main_page from "@/page/userpages/user_main_page.vue";
import user_unsafe_login from "@/page/userpages/user_unsafe_login.vue";
import user_unsafe_register from "@/page/userpages/user_unsafe_register.vue";
import ChatFloat from "@/page/ChatFloat.vue";
Vue.use(VueRouter);
 
const router = new VueRouter({
  
  routes: [
    { path: "/", redirect: "/hr_login" },
    { 
      path: "/hr_login", 
      component: hr_login,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/hr_register", 
      component: hr_register,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/hr_main_page", 
      component: hr_main_page,
      meta: { requiresAuth: true, role: 'hr' } 
    },
    { 
      path: "/user_login", 
      component: user_login,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/user_register", 
      component: user_register,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/user_unsafe_login", 
      component: user_unsafe_login,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/user_unsafe_register", 
      component: user_unsafe_register,
      meta: { requiresAuth: false, role: 'all' } 
    },
    { 
      path: "/user_main_page", 
      component: user_main_page,
      meta: { requiresAuth: true, role: 'user' } 
    },
    { 
      path: "/chat_float", 
      component: ChatFloat,
      meta: { requiresAuth: true, role: 'all' } 
    }
  ]
});

// 路由导航守卫 - 实现角色权限控制
router.beforeEach((to, from, next) => {
  // 获取目标路由的权限配置
  const { requiresAuth, role } = to.meta;
  
  // 检查是否需要认证
  if (requiresAuth) {
    // 检查用户是否已登录（HR或普通用户）
    const hrToken = localStorage.getItem('hr_token');
    const userToken = localStorage.getItem('user_token');
    
    // 判断当前用户角色
    let currentRole = null;
    if (hrToken && !userToken) {
      currentRole = 'hr';
    } else if (userToken && !hrToken) {
      currentRole = 'user';
    } else if (hrToken && userToken) {
      // 如果两个token都存在，根据目标页面角色选择使用哪个
      if (role === 'hr') {
        currentRole = 'hr';
      } else if (role === 'user') {
        currentRole = 'user';
      } else {
        // 对于允许所有角色的页面，保留两个token都存在的状态
        // 由axios拦截器根据路径决定使用哪个token
      }
    }
    
    // 检查角色权限
    if (role === 'hr' && (!currentRole || currentRole !== 'hr')) {
      alert('您没有权限访问该页面，需要以HR身份登录');
      next('/hr_login');
      return;
    } else if (role === 'user' && (!currentRole || currentRole !== 'user')) {
      alert('您没有权限访问该页面，需要以用户身份登录');
      next('/user_login');
      return;
    } else if (role === 'all' && !currentRole) {
      // 需要登录但不限制角色的页面，检查是否有任一token
      if (!hrToken && !userToken) {
        alert('请先登录');
        next('/hr_login');
        return;
      }
    }
  }
  
  // 允许访问
  next();
});

export default router;